﻿using ShSoft.Common.PoweredByLee;
using SlamDunk.CMS.Model.DataModel;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace SlamDunk.CMS.DAL
{
    public class MenusDal
    {
        #region # 静态构造器

        /// <summary>
        /// SqlHelper
        /// </summary>
        private static readonly SqlHelper _SqlHelper;

        /// <summary>
        /// 静态构造器
        /// </summary>
        static MenusDal()
        {
            _SqlHelper = new SqlHelper(ConfigurationManager.ConnectionStrings["CMS"].ConnectionString);
        }

        #endregion

        #region 01.根据父Id获取菜单列表
        /// <summary>
        /// 根据父Id获取菜单列表
        /// </summary>
        /// <param name="pId">父Id</param>
        /// <returns>菜单列表</returns>
        public List<Menus> GetModelList(int pId)
        {
            List<Menus> list = new List<Menus>();
            string sql = "SELECT * FROM Menus WHERE DelFlag = 0 AND PId = @PId";
            using (SqlDataReader reader = _SqlHelper.ExecuteReader(sql, new SqlParameter("@PId", pId)))
            {
                while (reader.Read())
                {
                    list.Add(this.ToModel(reader));
                }
            }
            return list;
        }
        #endregion

        #region 01.添加方法
        /// <summary>
        /// 添加一个实体对象
        /// </summary>
        /// <param name="menus">要添加的实体对象</param>
        /// <returns>添加完毕后的实体对象</returns>
        public Menus Add(Menus menus)
        {
            string sql = "INSERT INTO Menus (PId, MenuName, Url, IsLink, Level, Sort, Ico, DelFlag, AddTime)  output inserted.Id VALUES (@PId, @MenuName, @Url, @IsLink, @Level, @Sort, @Ico, @DelFlag, @AddTime)";
            SqlParameter[] args = new SqlParameter[] {
                new SqlParameter("@PId", ToDBValue(menus.PId)),
                new SqlParameter("@MenuName", ToDBValue(menus.MenuName)),
                new SqlParameter("@Url", ToDBValue(menus.Url)),
                new SqlParameter("@IsLink", ToDBValue(menus.IsLink)),
                new SqlParameter("@Level", ToDBValue(menus.Level)),
                new SqlParameter("@Sort", ToDBValue(menus.Sort)),
                new SqlParameter("@Ico", ToDBValue(menus.Ico)),
                new SqlParameter("@DelFlag", ToDBValue(menus.DelFlag)),
                new SqlParameter("@AddTime", ToDBValue(menus.AddTime)),
            };
            //根据newId返回新添加的实体对象
            int newId = (int)_SqlHelper.ExecuteScalar(sql, args);
            return this.GetModel(newId);
        }
        #endregion

        #region 02.删除方法（物理删除）
        /// <summary>
        /// 删除一个实体对象
        /// </summary>
        /// <param name="id">要删除的实体对象Id</param>
        /// <returns>受影响的行数</returns>
        public int PhysicalDelete(int id)
        {
            string sql = "DELETE FROM Menus WHERE Id = @Id";
            SqlParameter arg = new SqlParameter("@Id", id);
            return _SqlHelper.ExecuteNonQuery(sql, arg);
        }
        #endregion

        #region 04.修改方法
        /// <summary>
        /// 修改一个实体对象
        /// </summary>
        /// <param name="menus">构造好的要修改的实体对象</param>
        /// <returns>受影响的行数</returns>
        public int Update(Menus menus)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("UPDATE Menus SET PId = @PId");
            sql.Append(", MenuName = @MenuName");
            sql.Append(", Url = @Url");
            sql.Append(", IsLink = @IsLink");
            sql.Append(", Level = @Level");
            sql.Append(", Sort = @Sort");
            sql.Append(", Ico = @Ico");
            sql.Append(", DelFlag = @DelFlag");
            sql.Append(", AddTime = @AddTime");
            sql.Append(" WHERE Id = @Id");
            SqlParameter[] args = new SqlParameter[] {
                new SqlParameter("@Id", menus.Id)
                ,new SqlParameter("@PId", ToDBValue(menus.PId))
                ,new SqlParameter("@MenuName", ToDBValue(menus.MenuName))
                ,new SqlParameter("@Url", ToDBValue(menus.Url))
                ,new SqlParameter("@IsLink", ToDBValue(menus.IsLink))
                ,new SqlParameter("@Level", ToDBValue(menus.Level))
                ,new SqlParameter("@Sort", ToDBValue(menus.Sort))
                ,new SqlParameter("@Ico", ToDBValue(menus.Ico))
                ,new SqlParameter("@DelFlag", ToDBValue(menus.DelFlag))
                ,new SqlParameter("@AddTime", ToDBValue(menus.AddTime))
            };
            return _SqlHelper.ExecuteNonQuery(sql.ToString(), args);
        }
        #endregion

        #region 05.根据主键获取单个实体对象
        /// <summary>
        /// 根据主键获取单个实体对象
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns>单个实体对象</returns>
        public Menus GetModel(int id)
        {
            string sql = "SELECT * FROM Menus WHERE Id = @Id";
            using (SqlDataReader reader = _SqlHelper.ExecuteReader(sql, new SqlParameter("@Id", id)))
            {
                if (reader.Read())
                {
                    return ToModel(reader);
                }
                else
                {
                    return null;
                }
            }
        }
        #endregion

        #region 06.获取实体对象集合
        /// <summary>
        /// 获取实体对象集合
        /// </summary>
        /// <returns>实体对象集合</returns>
        public List<Menus> GetModelList()
        {
            List<Menus> list = new List<Menus>();
            string sql = "SELECT * FROM Menus WHERE DelFlag = 0";
            using (SqlDataReader reader = _SqlHelper.ExecuteReader(sql))
            {
                while (reader.Read())
                {
                    list.Add(ToModel(reader));
                }
            }
            return list;
        }
        #endregion

        #region 07.获取总记录条数
        /// <summary>
        /// 返回总记录条数
        /// </summary>
        /// <returns>总记录条数</returns>
        public int GetCount()
        {
            string sql = "SELECT COUNT(*) FROM Menus WHERE DelFlag = 0";
            return (int)_SqlHelper.ExecuteScalar(sql);
        }
        #endregion

        #region 17.根据IDataReader对象返回实体对象方法
        /// <summary>
        /// 根据IDataReader对象返回实体对象方法
        /// </summary>
        /// <param name="reader">IDataReader对象</param>
        /// <returns>实体对象</returns>
        public Menus ToModel(IDataReader reader)
        {
            Menus menus = new Menus();
            menus.Id = (int)ToModelValue(reader as SqlDataReader, "Id");
            menus.PId = (int)ToModelValue(reader as SqlDataReader, "PId");
            menus.MenuName = (string)ToModelValue(reader as SqlDataReader, "MenuName");
            menus.Url = (string)ToModelValue(reader as SqlDataReader, "Url");
            menus.IsLink = (bool?)ToModelValue(reader as SqlDataReader, "IsLink");
            menus.Level = (int)ToModelValue(reader as SqlDataReader, "Level");
            menus.Sort = (int?)ToModelValue(reader as SqlDataReader, "Sort");
            menus.Ico = (string)ToModelValue(reader as SqlDataReader, "Ico");
            menus.DelFlag = (bool)ToModelValue(reader as SqlDataReader, "DelFlag");
            menus.AddTime = (DateTime)ToModelValue(reader as SqlDataReader, "AddTime");
            return menus;
        }
        #endregion

        #region 18.C#值转数据库值空值处理
        /// <summary>
        /// C#值转数据库值空值处理
        /// </summary>
        /// <param name="value">C#值</param>
        /// <returns>处理后的数据库值</returns>
        public object ToDBValue(object value)
        {
            if (value == null)
            {
                return DBNull.Value;
            }
            else
            {
                return value;
            }
        }
        #endregion

        #region 19.数据库值转C#值空值处理
        /// <summary>
        /// 数据库值转C#值空值处理
        /// </summary>
        /// <param name="reader">IDataReader对象</param>
        /// <param name="columnName">列名</param>
        /// <returns>C#值</returns>
        public object ToModelValue(IDataReader reader, string columnName)
        {
            if (reader.IsDBNull(reader.GetOrdinal(columnName)))
            {
                return null;
            }
            else
            {
                return reader[columnName];
            }
        }
        #endregion
    }
}
